package ui.server;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import ui.client.ChatService;
import ui.client.Group;
import ui.client.InstantMessage;
import ui.client.UserPresence;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * the class of ChatServiceImpl is the implementation of ChatService interface.
 * 
 * @author wangkaidi
 *
 */
public class ChatServiceImpl extends RemoteServiceServlet implements ChatService
{
	private ConnectionManager cm=ConnectionManager.getInstance();
	private SimpleLogger logger=SimpleLogger.getInstance();
	
	/**
	 * try to login to the system
	 * if login success,return "true"
	 * else return "false"
	 */
	public String login(String protocolname,String username,String password,String domain,String host,int port)
	{
		String sessionid=this.getSessionid();		
		if(cm.exists(sessionid))
		{
			logger.log(SimpleLogger.FATAL, "session : "+sessionid+" has already a XMPPProtocolManager");
			cm.disconnect(sessionid);//disconnect and remove
			//return "false";
			cm.createManager(protocolname, username, password, domain, host, port, sessionid);//create a XMPPProtocolManager
			boolean result=cm.login(sessionid);
			if(result)return "true";
			else return "false";
		}
		else
		{
			cm.createManager(protocolname, username, password, domain, host, port, sessionid);//create a XMPPProtocolManager
			boolean result=cm.login(sessionid);
			if(result)return "true";
			else return "false";
		}
	}
	
	/**
	 * disconnect the session
	 */
	public void disconnect()
	{
		String sessionid=this.getSessionid();
		cm.disconnect(sessionid);
	}
	
	/**
	 * send message 
	 */
	public String send(InstantMessage message)
	{
		String sessionid=this.getSessionid();
		boolean b=cm.sendMessage(sessionid, message);
		if(b)return "true";
		else return "false";
	}
	
	public ArrayList<InstantMessage> receive()
	{
		String sessionid=this.getSessionid();
		logger.log(SimpleLogger.DEBUG, "receiving : "+sessionid);
		while(true)
		{
			if(cm.hasMessages(sessionid))//if has message
			{
				ArrayList<InstantMessage> list=cm.getMessages(sessionid);
				return list;
			}
			else//else wait and recheck
			{
				try
				{
					Thread.sleep(500);
				}
				catch(Exception e)
				{					
				}
			}	
		}
	}
	
	/**
	 * return the changed UserPresence
	 */
	public ArrayList<UserPresence> presenceChange()
	{
		String sessionid=this.getSessionid();
		logger.log(SimpleLogger.DEBUG,"presence change :"+sessionid);
		while(true)
		{
			if(cm.isPresenceChange(sessionid))
			{
				ArrayList<UserPresence>list=cm.getPresence(sessionid);
				return list;
			}
			else
			{
				try
				{
					Thread.sleep(500);
				}
				catch(Exception e)
				{					
				}
			}
		}
	}
	
	/**
	 * this method will return a list of Friend Group
	 */
	public ArrayList<Group> getGroups()
	{
		String sessionid=this.getSessionid();
		return cm.getGroup(sessionid);
	}
	
	/**
	 * return the servlet's session id
	 * @return
	 */
	private String getSessionid()
	{
		HttpSession session=this.getThreadLocalRequest().getSession();
		return session.getId();
	}
}
